Runs the script action once for each cell of target, handing it that cell's value. The cells come row by row: the whole top row from left to right, then the next row down, and so on. action is the NAME of one of your scripts and it must take exactly one argument.
grid_foreach(target, action)| Name | Type |
|---|---|
target | grid |
action | script |
Nothing.
Runs a script once for each cell of target, a grid. Each time, the script is handed the value in that cell. The cells come row by row: the whole top row from left to right, then the next row down, and so on to the bottom right.
grid_foreach(board, spawn_coin);The script is handed the value only, not where the cell is. If it needs the place, loop over the grid yourself with board[# x, y].
The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that makes a coin at a place across the screen:
// scripts/spawn_coin.gbml
function spawn_coin(x: int) {
instance_create(x, 20, obj_coin);
}The script has to follow three rules, and each one is checked before the game runs:
x: int. A grid of whole numbers fits a parameter that wants decimals, but a grid of decimals does not fit one that wants whole numbers. A script that is only used here can leave the type out: it takes the kind of the cells of the grid. Write it when you want it checked.You do not have to make a script file for a short job. Write the function right where the name would go:
var sum = 0;
grid_foreach(board, function (x) { sum += x; });It is the script, only without a name. Its one parameter takes the kind of the cells of the grid, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.
| You do this | What happens |
|---|---|
give a function written out in the middle of the line, like function (x) { ... } | that is fine: it is the script, only without a name (see "A function written out in place" above). It must take exactly one parameter, and it can use and change the variables around it |
give the name of a variable, or of a built-in function like instance_destroy | refused when checked. It has to be one of your scripts |
| give a script with no parameters, or with two or more | refused when checked |
| give a script whose parameter is a different kind from the values, like a text parameter for a grid of numbers | refused when checked, and the message says which kinds do not match |
| give it a list or an array | refused when checked, and the message names the function for that kind |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes a grid as a parameter works on the caller's own grid (see Grids). grid_foreach needs no width or height, so, like grid_fill, it works inside a script on the caller's grid, whatever size that is.
One call of your script for each cell of the grid, and nothing else. No memory is set aside.
var board = grid_create(3, 2, 0);
board[# 0, 0] = 20;
board[# 1, 0] = 60;
board[# 2, 0] = 100;
grid_foreach(board, spawn_coin); // six cells, so the script runs six timesA plain loop over every cell of the vector, which a grid stores row by row. It needs no width or height, so, like grid_fill, it also works inside a script on the CALLER's grid. The script is handed only the value, not where the cell is. The script is called once for each item, from the first place to the last, as an ordinary C++ call (`game::name(item)`) inside a loop that reads the length again every time round, so a script that changed the container it was walking could not make the loop read past the end. What the script gives back is ignored. The script is the name of one of your own scripts, or a function written out in the middle of the line (function (n) { ... }), which is written out as a C++ function where it stands and can see the variables around it; either way it must take exactly one argument whose type accepts the items (a function with none written takes the items' kind). It costs one call of the script per item and nothing else: no memory is set aside.